# coding=utf8
"""
dependency on module cv2: pip install opencv-python
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import os


class Preliminary:

    @classmethod
    def read_images(cls, path, filename='flower'):
        """
        从一个目录中读取多个图像文件数据
        返回存放图像数据的列表
        """
        images_path = [os.path.join(path, f) for f in os.listdir(path) if filename in f]
        images_data = []
        for fp in images_path:
            images_data.append(plt.imread(fp))
        return images_data

    @staticmethod
    def imshow(path='.', filename='flower01.jpg'):
        """显示图像的方式"""
        imgdata = plt.imread(os.path.join(path, filename))
        # print(imgdata.shape)

        # 缺省方式
        plt.figure(1)
        plt.imshow(imgdata)

        # 使用origin, 调整图像数据显示的顺序（起始点为左下角，方向向上）
        plt.figure(2)
        plt.imshow(imgdata, origin='lower')

        # 使用自适应坐标轴比例方式显示
        plt.figure(3)
        plt.imshow(imgdata, aspect='auto')
        plt.show()

    @staticmethod
    def imshow_images(path='.', filename='flower', row=2, col=3):
        images_data = Preliminary.read_images(path, filename)
        plt.figure('images-in-one')
        for i, image in enumerate(images_data):
            plt.subplot(int('{}{}{}'.format(row, col, i+1)))	    # 将第i个子图置为当前子图
            plt.imshow(image)		                                # 在当前子图中显示第i幅图像
        plt.show()

    @staticmethod
    def imshow_images_noticks(path='.', filename='flower', row=2, col=3):
        images_data = Preliminary.read_images(path, filename)
        plt.figure('image-in-one-noticks')
        for i, image in enumerate(images_data):
            plt.subplot(int('{}{}{}'.format(row, col, i+1)))	    # 将第i个子图置为当前子图
            plt.imshow(image)		                                # 在当前子图中显示第i幅图像
            plt.xticks([])			                                # 将x轴刻度置空
            plt.yticks([])			                                # 将y轴刻度置空
        plt.show()

    @staticmethod
    def images_concat(images, row, col):
        """
        按照第一个图像的形状改变其他图像形状，然后进行拼接
        :param images: 图像数量
        :param row: 行数
        :param col: 列数
        :return: imgall 拼接后的图像数据
        """
        width, height = images[0].shape[0:2][::-1]		                            # 第一个图像的宽度和高度
        concat_image = None
        for r in range(row):
            row_image = cv2.resize(images[r*col], (width, height))                  # 按第一个图像的大小，设置每行开始图像形状
            for c in range(1, col):
                img = cv2.resize(images[r*col+c], (width, height))                  # 改变大小拼接到行图像
                row_image = np.concatenate((row_image, img), axis=1)
            if concat_image is None:
                concat_image = row_image                                            # 第一行拼接的图像
            else:
                concat_image = np.concatenate((concat_image, row_image), axis=0)    # 拼接每行的图像
        return concat_image

    @staticmethod
    def imshow_images_concat(path='.', filename='flower', row=2, col=3):
        images = Preliminary.read_images(path, filename)
        big_image = Preliminary.images_concat(images, row, col)
        plt.figure('concat-images-to-one')
        plt.imshow(big_image)
        plt.show()


def task(path='.'):
    image_files = [os.path.join(path, 'flower0{}.jpg'.format(j+1)) for j in range(6)]
    image_data = [plt.imread(f) for f in image_files]
    image_concat = Preliminary.images_concat(image_data, 2, 3)
    plt.figure("images-in-one-axes")
    plt.imshow(image_concat)
    plt.show()


if __name__ == "__main__":
    # Preliminary.imshow()
    Preliminary.imshow_images()
    # Preliminary.imshow_images_noticks()
    # Preliminary.imshow_images_concat()
    # task()
    # exercise_1()
    # exercise_2()
